% "Repricing Avalanche" Online Appendix D.2, Model with constant money growth
% Baseline code "post_simulation.m" is modified
% Modified parts are marked by "%---".
% Obtain statistics of simulations as in the two-productivity model in Section 5
% Choose a file generated by simulations_r in line 11, and save results at
% the bottom.
% August 2022
% Makoto Nirei

clearvars;
% This code applies for the cases gamma_m>0
load variable_wage_01.mat;

%---- modified part: combine dlogP and dlogP_sub
sub_m(:,1) = ceil(dlogP_sub(:,6)/month_time); % month when the sub avalanche occurred
sub_m(:,2) = dlogP_sub(:,3) .* (dlogP_sub(:,3)>0)*delta...
    -dlogP_sub(:,3) .* (dlogP_sub(:,3)<0)*delta_u; % incurred menu costs
%----

dlogPm=zeros(maxmonth,pilength); L=zeros(maxmonth,pilength);    % monthly aggregate price increase
dlogPm(1,:)=sum(dlogP(1:calvom(1),:));                  % monthly price increase for the 1st month

%---- sum up price jumps between two Calvos and add it to the jump due to the first Calvo
subtmpP=sum(dlogP_sub(:,2).*(sub_m(:,1)==1));
dlogPm(1,:)=dlogPm(1,:)+subtmpP;
%----

idl=(nump(1:calvom(1),:)>0);                            % indicator for price rise
idlm=ones(calvom(1),pilength)-idl;                      % indicator for price cuts
cumcost_t = cumsum(nump.*(nump>1)*delta - nump.*(nump<-1)*delta_u); % cumulative menu costs
DL_t = cumcost_t -interp1([0; calvot],[zeros(1,pilength); cumcost_t],calvot-1/12,'previous'); % cumulated in the last month. 1st month will be discarded
mNpY=zeros(maxmonth-1,pilength);
mcons=mNpY; DL=mNpY;
pc1=(1-alpha_U*(1-gamma_U))/(1-alpha_U)/(1-gamma_U);
m_ss=zeros(1,pilength); c_ss=m_ss; n_ss=m_ss; w_ss=m_ss;
for i_pi=1:pilength
    ss=ss_pi{i_pi};
    m_ss(1,i_pi)=ss.m;
    c_ss(1,i_pi)=ss.C;
    n_ss(1,i_pi)=ss.N;
    w_ss(1,i_pi)=ss.w;
end

options = optimset('Display','off');
for i=2:maxmonth
    dlogPm(i,:)=sum(dlogP(calvom(i-1)+1:calvom(i), :)); % monthly price increase

    %---- add price jumps happened during inter-Calvo periods in month i
    subtmpP=sum(dlogP_sub(:,2).*(sub_m(:,1)==i));
    dlogPm(i,:)=dlogPm(i,:)+subtmpP;
    %----

    idl=(nump(calvom(i-1)+1:calvom(i),:)>0);            % indicator for price rise
    idlm=ones(calvom(i)-calvom(i-1),pilength)-idl;      % indicator for price cut
    DL(i-1,:)=DL_t(calvom(i-1)+1,:);

    %---- add menu costs incurred during inter-Calvo periods in month i
    subtmpD= sum(sub_m(:,2).*(sub_m(:,1)==(i-1)));
    DL(i-1,:) = DL(i-1,:)+subtmpD;
    %----

    mNpY(i-1,:) = (sum(NpY(calvom(i-1)+1:calvom(i),:).*diff(calvot(calvom(i-1)+1:calvom(i)+1)))...
        +NpY(calvom(i-1),:)*(calvot(calvom(i-1)+1)-month_time*(i-1))...
        -NpY(calvom(i),:)*(calvot(calvom(i)+1)-month_time*i))/month_time; % monthly averaged NpY
    cc0=(1-DL(i-1,:)/n/month_time)./mNpY(i-1,:);
    cc1=cc0./(m_ss*rho*alpha_U).^(1/(1-alpha_U)/(1-gamma_U));
    compu_c = @(x) x -cc0 + cc1.*x.^pc1;
    mcons(i-1,:)=fsolve(compu_c, c_ss, options);
end

dlogPy=zeros(maxmonth-11,pilength);
for i=1:maxmonth-11
    dlogPy(i,:)=sum(dlogPm(i:i+11,:));                  % monthly, year-on-year price increase
end
stdP = std(dlogPy);                                     % volatility of annual inflation
meanP = mean(dlogPy);                                   % average of annual inflation

% stats for various pi
disp('pi, average infl, std infl, negative L');
disp([piv meanP stdP negnump]);

disp('std of log(c) for various pi');
disp(std(log(mcons)));

disp('std of monthly log(N/Y)');
disp(std(log(mNpY)));

disp('mean, std, min, max of monthly d(log(P))');
disp([mean(dlogPm(:,:)); std(dlogPm(:,:)); min(dlogPm(:,:)); max(dlogPm(:,:))]);

% ratio of the effect of w to the Calvo
disp('frequency of repricing due to w relative to Calvo')
disp(length(dlogP_sub)/length(dlogP));

dlogm=(1:length(dlogPm))*piv/12 -cumsum(dlogPm');
disp('std(log w)');
disp(std(dlogm)*gamma_m);

disp('frequency of L=0 after a Calvo');
disp(sum(abs(nump)==1)/length(nump));

save nump_01 nump;